package com.yzw.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzw.common.domain.EduStatisticsDaily;
import com.yzw.common.mapper.EduMemberMapper;
import com.yzw.common.mapper.EduStatisticsDailyMapper;
import com.yzw.statistics.service.EduStatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class EduStatisticsDailyServiceImpl extends ServiceImpl<EduStatisticsDailyMapper, EduStatisticsDaily>
        implements EduStatisticsDailyService {
    @Resource
    private EduMemberMapper eduMemberMapper;

    @Override
    public Integer saveStatistics(String day) {
        QueryWrapper<EduStatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);

        EduStatisticsDaily eduStatisticsDaily = new EduStatisticsDaily();
        Integer count = eduMemberMapper.countRegisterDay(day);
        eduStatisticsDaily.setRegisterNum(count);
        eduStatisticsDaily.setDateCalculated(day);


        eduStatisticsDaily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        eduStatisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200));
        eduStatisticsDaily.setLoginNum(RandomUtils.nextInt(100, 200));

        baseMapper.insert(eduStatisticsDaily);
        return count;
    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<EduStatisticsDaily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated", begin, end);
        queryWrapper.select("date_calculated", type);
        List<EduStatisticsDaily> eduStatisticsDailies = baseMapper.selectList(queryWrapper);
        List<String> dateCalculated = new ArrayList<>();
        List<Integer> numList = new ArrayList<>();
        for (int i = 0; i < eduStatisticsDailies.size(); i++) {
            EduStatisticsDaily eduStatisticsDaily = eduStatisticsDailies.get(i);
            dateCalculated.add(eduStatisticsDaily.getDateCalculated());
            if (type.equals("login_num")){
                numList.add(eduStatisticsDaily.getLoginNum());
            }
            if (type.equals("register_num")){
                numList.add(eduStatisticsDaily.getRegisterNum());
            }
            if (type.equals("video_view_num")){
                numList.add(eduStatisticsDaily.getVideoViewNum());
            }
            if (type.equals("course_num")){
                numList.add(eduStatisticsDaily.getCourseNum());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dateCalculated",dateCalculated);
        map.put("numList",numList);
        return map;
    }
}
